<?php
/*
 * Aclass_nav 导航类
 * 2010-07-08
 * http://www.abans.cn		abans@qq.com
 */
class Aclass_html{
	function  __construct() {
	}
	


	function tag($type,$arr='',$html=''){
		$adds = '</'.$type.'>';
		if(in_array($type,array('input','br','hr'))){
			$adds = '';
		}
		$str = $this->unite($arr);
		$strs = '<'.$type.' '.$str.'>'.$html.$adds;
		if(is_array($arr) && $arr['type'] == 'text'){
			$strs = $html.'<'.$type.' '.$str.'>'.$adds;
		}
		if($type == 'textarea'){
			$strs = '<'.$type.' '.$str.'>'.$html.$adds;
		}
		return $strs;
	}
	function input($type,$val,$key){
		$arr = array(
			'type'=>$type,
			'id'=>'save_'.$key,
			'name'=>'save['.$key.']',
			'value'=>$val
			);
		return $this->tag('input',$arr);
	}
	function inputs($obj,$data='',$val='',$label=''){
		$str = '';
		if(!is_array($data) && !is_object($data)){return;}
		foreach($data as $k=>$v){
			unset($obj['checked']);
			$obj['value'] = $k;
			if(is_array($val)){
				if(in_array($k,$val)){$obj['checked'] = 'checked';}
			}else if(is_string($val)){
				if($val == $k){$obj['checked'] = 'checked';}
			}
			$str .= $this->tag('input',$obj, $this->tag('span','',$v));
		}
		return $label.$str;
	}
	function select($obj,$data='',$val='',$label=''){
		$option = '';
		foreach($data as $k=>$v){
			$opstr = array();
			$opstr['value'] = $k;
			if($k == $val && strlen($k)==strlen($val)){$opstr['selected'] = 'selected';}
			$option .= $this->tag('option',$opstr,$v);
		}
		return $label.$this->tag('select',$obj,$option);
	}
	function unite($arr){
		if(!is_array($arr) && !is_object($arr)){return;}
		$str = "";
		foreach($arr as $k=>$v){
			$str .= $k.'="'.$v.'" ';
		}
		return $str;
	}

	function form($type='text',$name,$value,$arr="",$v=""){
		if(is_array($arr))
			foreach($arr as $key=>$val){
				if(empty($htm))
					$htm = $key.'="'.$val.'"';
				else
					$htm .= ' '.$key.'="'.$val.'"';
			}
		if(in_array($type,array('text','button','submit','file','hidden','password','reset','image'))){
			$html = '<input type="'.$type.'" name="'.$name.'" value="'.$value.'" '.$htm.' />';
		}else{
			switch ($type){
				case 'radio':
					foreach($value as $key=>$val){
						if($v==$key && strlen($key)==strlen($v)){	$checked = 'checked="checked"';}
						$html .= '<input type="'.$type.'" name="'.$name.'" value="'.$key.'" '.$htm.' '.$checked.' /><span>'.$val.'</span>';
						unset($checked);
					}
					break;
				case 'checkbox':
					foreach($value as $key=>$val){
						if(!empty($v) && is_array($v)){ if(in_array($key,$v)){$checked = 'checked="checked"';}}
						$html .= '<input type="checkbox" name="'.$name.'['.$key.']" value="'.$key.'" '.$htm.' '.$checked.' /><span>'.$val.'</span>';
						unset($checked);
					}
					break;
				case 'select':
					$html = '<select name="'.$name.'" '.$htm.' >';
					if(is_array($value) || is_object($value))
					foreach($value as $key=>$val){
						if($key==$v && strlen($key)==strlen($v)){	$selected = 'selected="selected"';}
						$html .= '<option value="'.$key.'" '.$selected.' >'.$val.'</option>';
						unset($selected);
					}
					$html .= '</select>';
					break;
			}
		}
		return $html;
	}

}

?>
